package edu.wpi.cs509.ui.simulators;

import java.util.ArrayList;
import java.util.List;
import java.util.TimerTask;

import edu.wpi.cs509.representation.GameListItem;
import edu.wpi.cs509.ui.intfc.ClientLogicNotify;

public class GamesNotify extends TimerTask {

	private ClientLogicNotify ui;
	private ClientLogicSimulator sim;
	
	public GamesNotify( ClientLogicNotify ui, ClientLogicSimulator sim ) {
		this.ui = ui;
		this.sim = sim;
	}
	
	@Override
	public void run() {
		List<ServerGame> games = sim.getAllGames();
		List<GameListItem> jList = new ArrayList<GameListItem>();
		
		GameListItem item;
		for (ServerGame g : games) {
			if (g.isJoinable()) {
				item = new GameListItem( g.getGameID(), g.getPlayer1Name() );
				jList.add( item );
			} else if (g.isObservable()) {
				item = new GameListItem( g.getGameID(), g.getPlayer1Name(), g.getPlayer2Name() );
				jList.add( item );
			}
		}
		
		ui.notifyGames(jList);
	}

}
